package linc.spring.demo;

import linc.spring.demo.bean.Role;
import linc.spring.demo.conversion.StringToRoleConverter;
import linc.spring.demo.editor.StringToRolePropertyEditor;
import org.junit.Test;
import org.springframework.beans.SimpleTypeConverter;
import org.springframework.core.convert.support.DefaultConversionService;

/**
 * @author yqlin
 * @date 2021/9/7 01:15
 * @description
 */
public class SpringConverterTest {
    @Test
    public void testConversion(){
        DefaultConversionService conversionService = new DefaultConversionService();
        conversionService.addConverter(new StringToRoleConverter());
        Role role = conversionService.convert("linc", Role.class);
        System.out.println(role);
    }

    @Test
    public void testTypeConverter(){
        SimpleTypeConverter converter = new SimpleTypeConverter();
        converter.registerCustomEditor(Role.class, new StringToRolePropertyEditor());
        Role role = converter.convertIfNecessary("linc", Role.class);
        System.out.println(role);
    }
}
